package com.example.personnel_oa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.comment_oa.dubbo.SysUserDubbo;
import com.example.comment_oa.pojos.SysEmployee;
import com.example.comment_oa.pojos.SysEmployeePost;
import com.example.comment_oa.pojos.SysPost;
import com.example.comment_oa.pojos.SysUser;
import com.example.comment_oa.pojos_vo.HySysEmpVo;
import com.example.personnel_oa.mapper.SysEmployeeMapper;
import com.example.personnel_oa.mapper.SysEmployeePostMapper;
import com.example.personnel_oa.mapper.SysPostMapper;
import com.example.personnel_oa.service.SysEmployeeService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author asus
* @description 针对表【sys_employee】的数据库操作Service实现
* @createDate 2022-10-17 19:09:25
*/
@Service
public class SysEmployeeServiceImpl extends ServiceImpl<SysEmployeeMapper, SysEmployee>
implements SysEmployeeService {
    @Autowired
    SysEmployeeMapper employeeMapper;
    @Autowired
    SysEmployeePostMapper employeePostMapper;
    @Autowired
    SysPostMapper postMapper;



    @Override
    public List<SysEmployeeMapper> selectDPE() {
        List<SysEmployeeMapper> employeeMappers = employeeMapper.selectDPE();
        return employeeMappers;
    }

    @Override
    public int countemp() {
        int countemp = employeeMapper.countemp();
        return countemp;
    }

    //根据id查询员工信息部门职位
    @Override
    public SysEmployee selectEmpId(int empId) {
        SysEmployee sysEmployee = employeeMapper.selectEmpId(empId);
        return sysEmployee;
    }

    //查询人事部门的员工//发布人下拉框数据
    @Override
    public List<SysEmployee> jobName() {
        List<SysEmployee> sysEmployees = employeeMapper.jobName();
        return sysEmployees;
    }

    //查询出全部员工不包含懂事长部门以下
    @Override
    public List<SysEmployee> selectEmp() {
        List<SysEmployee> sysEmployees = employeeMapper.selectEmp();
        return sysEmployees;
    }

    //条件查询（部门id,职位id，性别，政治面貌，婚姻，实习/正式）
    @Override
    public List<SysEmployee> selectEmpPostDept(HySysEmpVo sysEmpVo) {
        List<SysEmployee> sysEmployees = employeeMapper.selectEmpPostDept(sysEmpVo);
        return sysEmployees;
    }

    //根据员工id修改员工工资
    @Override
    public int updateSalary(int empSalary, int empId) {
        int i = employeeMapper.updateSalary(empSalary, empId);
        return i;
    }

    //根据部门id查询全部员工
    @Override
    public List<SysEmployee> depidtEmp(int deptId) {
        List<SysEmployee> sysEmployees = employeeMapper.depidtEmp(deptId);
        return sysEmployees;
    }


    @Override
    public SysEmployee gonghao(String empCard) {
        //根据工号查询员工id（ SELECT * FROM sys_employee WHERE EMP_CARD ='TR1019' ）
        SysEmployee gonghao = employeeMapper.gonghao(empCard);
        //根据员工id查询职位id（ SELECT * from sys_employee_post WHERE EMP_ID=20）
        SysEmployeePost empid = employeePostMapper.empid(gonghao.getEmpId());
        //根据职位id查询对应的上级职位id（ SELECT * FROM sys_post WHERE POST_ID = 14）
        SysPost post = postMapper.post(empid.getPostId());
        //上级职位id
        int id = post.getPostUp();
        //根据上级职位id查询对应的上级员工id（SELECT * from sys_employee_post WHERE POST_ID=13）
        SysEmployeePost sysEmployeePost = employeePostMapper.postId(post.getPostUp());
        //当查到上级职位id对应的empid
        if (sysEmployeePost != null){
            //根据empid查询上级信息
            SysEmployee sysEmployee = employeeMapper.depidtEmp2(sysEmployeePost.getEmpId());
            return sysEmployee;
        } else {
            //当查不到上级职位id对应的empid员工
            //一个死循环
            for (;;) {
                //返回在查上级职位id
                SysPost post2 = postMapper.post(id);
                //查到上级职位的上上级职位id
                SysEmployeePost sysEmployeePost2 = employeePostMapper.postId(post2.getPostUp());
                //根据上级员工id查出对应上级信息
                //（ SELECT * from sys_employee e LEFT JOIN sys_employee_post s on e.EMP_ID = s.EMP_ID WHERE s.EMP_ID = 13）
                SysEmployee sysEmployee2 = employeeMapper.depidtEmp2(sysEmployeePost2.getEmpId());
                if(sysEmployeePost2 != null){
                    //查到了数据就返回退出循环
                    return sysEmployee2;
                }
            }
        }
    }

    //根据工号查询全部员工信息(部门，岗位)
    @Override
    public SysEmployee empCard_empId(String empCard) {
        SysEmployee sysEmployee = employeeMapper.empCard_empId(empCard);
        return sysEmployee;
    }
        //员工入职时新增员工
        @Override
        public int addEmp(SysEmployee employee){
            int i = employeeMapper.addEmp(employee);
            return i;
        }

        //入职时添加员工职位中间表，先查出员工id
        @Override
        public SysEmployee ID(String empIdcard){
            SysEmployee id = employeeMapper.ID(empIdcard);
            return id;

        }

    //查询在职，离职工资未结清的人员
    @Override
    public List<SysEmployee> empis() {
        List<SysEmployee> empis = employeeMapper.empis();
        return empis;
    }

    //根据部门ID and 职位ID查询员工
    @Override
    public SysEmployee empCardID(String empCard) {
        SysEmployee sysEmployee = employeeMapper.empCardID(empCard);
        return sysEmployee;
    }


    //根据岗位名称查询员工
    @Override
    public List<SysEmployee> postName(String postsName) {
        List<SysEmployee> sysEmployee = employeeMapper.postName(postsName);
        return sysEmployee;

    }
    //根据员工id修改试用员工为正式员工
    @Override
    public int setEmpState(int empId) {
        int i = employeeMapper.setEmpState(empId);
        return i;

    }
}